<?php
session_start();
require_once '../data/connect.class.php';
require_once '../data/pager.class.php';
include("../fckeditor/fckeditor.php") ;
$connect = new Connect("localhost","root","","email");
$param = $_POST['param'];

if (isset($param)) {
	
	if ($param=='templatelist') {
		$querySql="select * from admin_mail_template;";
		$resultSet = $connect->execute($querySql);
		echo "<div class='page_user_row'>";
		echo "  <div class='page_template_content'>";
		echo "     <strong>模板名</strong>";
		echo "  </div>";
		echo "  <div class='page_template_content'>";
		echo "    <strong>模板地址</strong>";
		echo "  </div>";
		echo "  <div class='page_template_content'>";
		echo "    <strong>创建者</strong>";
		echo "  </div>";
		echo "  <div class='page_template_content'>";
		echo "    <strong>创建时间</strong>";
		echo "  </div>";
		echo "  <div class='page_template_content'>";
		echo "    <strong>操作</strong>";
		echo "  </div>";
		echo "</div>";
		foreach($resultSet as $res) {
			echo "<div class='page_user_row'>";
			echo "  <div class='page_template_content'>";
			echo $res['name'];
			echo "  </div>";
			echo "  <div class='page_template_content'>";
			echo "<a href='".$res['template_path']."' target=_black>click</a>";
			echo "  </div>";
			echo "  <div class='page_template_content'>";
			echo $res['creator_name'];
			echo "  </div>";
			echo "  <div class='page_template_content'>";
			echo date("Y-m-d h-i-s a", $res['create_time']);
			echo "  </div>";
			echo "  <div class='page_template_content' onclick='delTemplate(\"";
			echo $res['id'];
			echo "\")' style='cursor:pointer;'>";
			echo "删除";
			echo "  </div>";
			echo "</div>";
		}
	} elseif ($param=='templatemanage') {
		echo "<label>模板名</label>"; 
		echo "<input type='text' id='template_name' value='' /><br/>"; 
		echo "<label>创建模板</label><br/>"; 
		$sBasePath = $_SERVER['PHP_SELF'] ;
		 
		$sBasePath = substr( $sBasePath, 0, strpos( $sBasePath, "pages" ) ) ;
		$sBasePath .= "fckeditor/";
		$oFCKeditor = new FCKeditor('newTemplate') ;
		$oFCKeditor->BasePath	= $sBasePath ;
		$oFCKeditor->Height = 600;
		$oFCKeditor->Value		= '' ;
		$oFCKeditor->Create() ;
	    echo "<div onclick='addTemplate()' style='cursor:pointer;'>确定</div>";
	} elseif ($param=='tasklist') {
		$querySql="select * from admin_task";
		$resultSet = $connect->execute($querySql);
		
		echo "<div class='page_user_row'>";
		echo "  <div class='page_task_content'>";
		echo "     <strong>任务名</strong>";
		echo "  </div>";
		echo "  <div class='page_task_content'>";
		echo "    <strong>创建者</strong>";
		echo "  </div>";
		echo "  <div class='page_task_content'>";
		echo "    <strong>创建时间</strong>";
		echo "  </div>";
		echo "  <div class='page_task_content'>";
		echo "    <strong>模板名</strong>";
		echo "  </div>";
		echo "  <div class='page_task_content' style='width: 180px;'>";
		echo "    <strong>回复地址</strong>";
		echo "  </div>";	
//		echo "  <div class='page_task_content'>";
//		echo "    <strong>服务器</strong>";
//		echo "  </div>";
		echo "  <div class='page_task_content' style='width: 74px'>";
		echo "    <strong>状态</strong>";
		echo "  </div>";
//		echo "  <div class='page_task_content' style='width: 70px'>";
//		echo "    <strong>操作</strong>";
//		echo "  </div>";
		echo "</div>";
		foreach($resultSet as $res) {
			echo "<div class='page_user_row'>";
			echo "  <div class='page_task_content'>";
			echo $res['name'];
			echo "  </div>";
			echo "  <div class='page_task_content'>";
			echo $res['creator_name'];
			echo "  </div>";
			echo "  <div class='page_task_content'>";
			echo date("Y-m-d h-i-s a", $res['create_time']);
			echo "  </div>";
			echo "  <div class='page_task_content'>";
			echo $res['template_name'];
			echo "  </div>";
			echo "  <div class='page_task_content' style='width: 180px;'>";
			echo $res['reply_name']."&lt;".$res['reply_addr']."&gt;";
			echo "  </div>";
//			echo "  <div class='page_task_content'>";
//			echo $res['smtp_server_name'];
//			echo "  </div>";
			echo "  <div class='page_task_content' style='width: 74px'>";
			echo $res['status']?"运行":"停止";
			echo "  </div>";
//			echo "  <div class='page_task_content' onclick='runTask(\"";
//			echo $res['id'];
//			echo "\")' style='cursor:pointer;width: 70px'>";
//			echo "运行";
//			echo "  </div>";
			echo "</div>";
		}
	} elseif ($param=='taskmanage') {
		echo "<img id='loading' src='../images/loading.gif' style='display:none;'>";
		echo "<label>任务名:</label>"; 
		echo "<input type='text' id='task_name' value='' /><br/><br/>"; 
		echo "<label>选择模板:</label>"; 
		$sql = "select * from admin_mail_template";
		$resultSet = $connect->execute($sql);
		echo "<select id='template_list'>";
		foreach ($resultSet as $res) {
			echo "<option value=";
            echo $res['id'];
            echo ">";
            echo $res['name'];
            echo "</option>";
		}

        echo "</select><br/><br/>";
        
        echo "<label>选择组:</label><br/>";
        $sql = "select * from admin_addr_class";
        $resultSet = $connect->execute($sql);
		foreach ($resultSet as $res) {
			echo "<input type='checkbox' name='classCheckbox' value='".$res['id']."'>";
        	echo $res['name']."<br/>";
		}
		
	    echo "<br/><label>选择域:</label><br/>";
        $sql = "select distinct domain from admin_mail_addr";
        $resultSet = $connect->execute($sql);
		foreach ($resultSet as $res) {
			echo "<input type='checkbox' name='domainCheckbox' value='".$res['domain']."'>";
        	echo $res['domain']."<br/>";
		}
        
        echo "<br/><label>选择用户:</label><br/>"; 
        
//        $sql = "select * from admin_mail_addr where subcripted=1";
//		$resultSet = $connect->execute($sql);
		
        echo "<div id='userSource' style='border: 1px solid #dcdcdc;width: 275px; height: 330px;float:left;margin-left: 10px;'>";
        userPager($connect);
        echo "</div>";
        
        echo "<div id='selectOpt' style='width: 100px; height: 330px; float:left;'>";
        echo "<div id='moveRight' style='cursor:pointer;text-align: center;margin-top:130px;' onclick='addAddrToSelector()'>&gt;&gt;</div>";
        echo "<div id='moveleft' style='cursor:pointer;text-align: center;' onclick='removeAddrFromSelector()'>&lt;&lt;</div>";
        echo "</div>";
        echo "<div id='userDest' style='border: 1px solid #dcdcdc;width: 275px; height: 330px;float:left;'>";
        echo "    <select multiple id='selectDest' style='width: 275px; height: 305px;'>";
        
        echo "    </select>";
        echo "</div>";
        
//		echo "<input type='checkbox' id='allSelector' onclick='selectAll()'>全选<br/>";
//		foreach ($resultSet as $res) {
//			echo "<input type='checkbox' name='checkbox' value='".$res['id']."'>";
//        	echo $res['name']."&lt;".$res['mail_addr']."&gt;<br/>";
//		}
		echo "<br/><label>回复名:</label>"; 
		echo "<input type='text' id='reply_name' value='' /><br/><br/>"; 		
		echo "<label>回复地址:</label>"; 
		echo "<input type='text' id='reply_addr' value='' /><br/><br/>"; 
//		echo "<label>选择服务器:</label>"; 
//		$sql = "select * from admin_smtp_server where status <> 0";
//		$resultSet = $connect->execute($sql);
//		echo "<select id='smtp_list'>";
//		foreach ($resultSet as $res) {
//			echo "<option value=";
//            echo $res['id'];
//            echo ">";
//            echo $res['name'];
//            echo "</option>";
//		}
//		echo "</select><br/><br/>";
		echo "<div onclick='addTask()' style='cursor:pointer;'>确定</div>";
	} elseif ($param=='userPager') {
		userPager($connect);
	}
}

function userPager($connect) {
	$url = $_SERVER['PHP_SELF'];
	$currentId = 1;
	$pageSize = 5;
	$sql = "select * from admin_mail_addr where subcripted=1";
	
    if (isset($_POST['currentId']) && $_POST['currentId'] > 0) {
		$currentId = $_POST['currentId'];
	}
    if (isset($_POST['pageSize']) && $_POST['pageSize'] > 0) {
		$_SESSION['pageSize']=$_POST['pageSize'];
	}
	
    if (isset($_SESSION['pageSize'])) {
		$pageSize = $_SESSION['pageSize'];
	}
	
	$array = array();
	$array['pageSize'] = $pageSize;
	$array['sql']=$sql;
	$array['currentId']=$currentId;
	$array['db']=$connect;
	$pager = new Pager($array);
	
    $pageData = $pager->getPageData();
    $resultSet = $pageData['pageData'];
    echo "<select multiple id='selectSource' style='width: 275px; height: 305px;'>";
    foreach($resultSet as $res) {
        echo "<option value=".$res['id'].">";
        echo $res['name']."&lt;".$res['mail_addr']."&gt;</option>";
    }
    echo "</select>";
    
    echo "<div style='float:right;'>";

    if (!$pageData['isFirstPage']) {
    	echo "<div style='float:left;cursor:pointer;' onclick='addAddrPager(\"".$url."\",\"userPager\",1,\"\")'>首页</div>&nbsp;";
		echo "<div style='float:left;cursor:pointer;' onclick='addAddrPager(\"".$url."\",\"userPager\",";
		echo $pageData['lastPage'];
		echo ",\"\")'>上一页</div>&nbsp;";
		
	}
//	for ($i=1; $i < $pageData['totalPages']+1; $i++) {
//		if ($i == $pageData['currentPage']) {
//			echo "<div style='color:red;float:left'>".$i."</div>&nbsp;";
//		} else {
//			echo "<div style='float:left;cursor:pointer;' onclick='addAddrPager(\"".$url."\",\"userPager\",".$i.",\"\")'>".$i."</div>&nbsp;";
//		}
//		
//	}
    
    if (!$pageData['isLastPage']) {
    	echo "<div style='float:left;cursor:pointer;' onclick='addAddrPager(\"".$url."\",\"userPager\",";
		echo $pageData['nextPage'];
		echo ",\"\")'>下一页</div>&nbsp;";
		echo "<div style='float:left;cursor:pointer;' onclick='addAddrPager(\"".$url."\",\"userPager\",".$pageData['totalPages'].",\"\")'>尾页</div>&nbsp;";
	}
	echo "</div>";
}
?>